Public Class Game
    Public Shared GameForm As Form
    Private objBack As Sprite
    Private objBack1 As Sprite
    Private objMario As Mario
    Private objEnemy As Enemy

    Private foreGround As Map

    

    Public running As Boolean

    Private lastTime As Long

    Public Sub New(ByVal frm As Form)
        'Set the form that we'll draw to
        GameForm = frm
        'This means that whenever Gameform.Paint is called (with a gameform.Invalidate),
        'Me.paint will also be called.
        AddHandler GameForm.Paint, AddressOf Me.Paint
        AddHandler GameForm.KeyDown, AddressOf Me.Keydown
        AddHandler GameForm.KeyUp, AddressOf Me.KeyUp

        foreGround = New Map

        objBack1 = New Sprite("../../Content/Images/Background1.png", New Point(0, 0), Color.Chartreuse)
        objBack = New Sprite("../../Content/Images/Background.png", New Point(0, 0), Color.Chartreuse)
        objMario = New Mario("../../Content/Images/Smario.png", New Point(100, 340), Color.Chartreuse, foreGround)
        objEnemy = New Enemy("../../Content/Images/koopa1.png", New Point(300, 340), Color.Chartreuse, foreGround)
    End Sub

    Public Sub run()
        running = True
        lastTime = Now.Ticks
        Do While running
            GameForm.Invalidate()
            Application.DoEvents()
        Loop
    End Sub

    Public Sub Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        objBack1.Draw(e)
        objBack.Draw(e)
        foreGround.Draw(e)
        objEnemy.Draw(e, Now.Ticks - lastTime)
        objMario.Draw(e, Now.Ticks - lastTime)
        e.Graphics.DrawString("(" & Math.Abs(objMario.location.X) / 32 & "," & Math.Abs(objMario.location.Y) / 32 & ") (" & Math.Abs(objMario.location.X + objMario.Bitmap.Width) / 32 & "," & Math.Abs(objMario.location.Y + objMario.Bitmap.Height) / 32 & ")", New Font("Courier New", 12), Brushes.Black, 0, 0)
        e.Graphics.DrawString(Math.Floor(Math.Abs(objMario.location.X + objMario.Bitmap.Width * 0.5) / 32) & ", " & Math.Floor(Math.Abs(objMario.location.Y + objMario.Bitmap.Height * 0.5) / 32), New Font("Courier New", 12), Brushes.Black, 0, 12)
        lastTime = Now.Ticks
    End Sub

    Public Sub Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.Up
                objMario.State = Mario.MarioState.Jumping
            Case Keys.Down
                'objMario.State = Mario.MarioState.Ducking
                'objmario.ducks()
            Case Keys.Left
                objMario.direction = Mario.SpriteDirection.Left
                objMario.State = Mario.MarioState.Walking
                objMario.moving = True
                objMario.smoving()
            Case Keys.Right
                objMario.direction = Mario.SpriteDirection.Right
                objMario.State = Mario.MarioState.Walking
                objMario.moving = True
                objMario.smoving()
        End Select
    End Sub

    Public Sub Keyup(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.Up
                objMario.stopjump()
                If objMario.moving = False Then
                    objMario.State = Mario.MarioState.Standing
                Else
                    objMario.State = Mario.MarioState.Walking
                End If
            Case Keys.Down
                objMario.State = Mario.MarioState.Standing
            Case Keys.Left
                If objMario.direction = Mario.SpriteDirection.Left Then
                    'objMario.State = Mario.MarioState.Stopping
                    objMario.stoporslow()
                End If
            Case Keys.Right
                If objMario.direction = Mario.SpriteDirection.Right Then
                    'objMario.State = Mario.MarioState.Stopping
                    objMario.stoporslow()
                End If
        End Select
    End Sub

End Class
